package hb.watcher.monitor;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SetupActivity extends Activity{

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	super.setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    	
    	setContentView(R.layout.setup);
    	
    	final Config conf = new Config();
    	conf.load();
    	
    	EditText edUrl =  (EditText)findViewById(R.id.Url);
    	EditText edDbName =  (EditText)findViewById(R.id.Database);
    	EditText edUserName =  (EditText)findViewById(R.id.Username);
    	EditText edPassword =  (EditText)findViewById(R.id.Password);

    	edUrl.setText(conf.get("url"));
    	edDbName.setText(conf.get("DBName"));
    	edUserName.setText(conf.get("UserName"));
    	edPassword.setText(conf.get("Password"));
    	
    	Button btn = (Button) findViewById(R.id.button1);
    	btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	
            	EditText edUrl =  (EditText)findViewById(R.id.Url);
            	EditText edDbName =  (EditText)findViewById(R.id.Database);
            	EditText edUserName =  (EditText)findViewById(R.id.Username);
            	EditText edPassword =  (EditText)findViewById(R.id.Password);
            	
            	
            	conf.set("url",edUrl.getText().toString());
                conf.set("DBName",edDbName.getText().toString());
                conf.set("UserName",edUserName.getText().toString());
                conf.set("Password",edPassword.getText().toString());
                
                conf.store();
                
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    

    	
    	
     Button test = (Button) findViewById(R.id.test);
     test.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
        	
        	EditText edUrl =  (EditText)findViewById(R.id.Url);
         	EditText edDbName =  (EditText)findViewById(R.id.Database);
         	EditText edUserName =  (EditText)findViewById(R.id.Username);
         	EditText edPassword =  (EditText)findViewById(R.id.Password);
         	
         	if(notEmpty(edUrl.getText().toString()) && 
         			notEmpty(edDbName.getText().toString()) &&
         					notEmpty(edUserName.getText().toString()) &&
         					notEmpty(edPassword.getText().toString()))
         	{
        	
         		DBConnection test = new DBConnection(edUrl.getText().toString(),edDbName.getText().toString(), edUserName.getText().toString(), edPassword.getText().toString());
         		
         		if(test.Connect())
         			Toast.makeText(getApplicationContext(), "Connection ok", Toast.LENGTH_LONG).show();
         		else
         			Toast.makeText(getApplicationContext(), "Not able to connect please check informations", Toast.LENGTH_LONG).show();
             	
         		if(test.IsConnected())
         			test.CloseConnection();
         		
         		
         	}else
         	{
         		Toast.makeText(getApplicationContext(), "Please fill in all informations", Toast.LENGTH_SHORT).show();
         	}
         }
     
     	});
    	
    }
    
    public  boolean notEmpty(String s) {
		 return (s != null && s.length() > 0);
		}
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
